/*
 * @Author: wangkg wangkegui@longfor.com
 * @Date: 2024-06-17 16:40:12
 * @Description:
 */
import { getAppEnvConfig } from '@/utils/env';
import { setAppUserTokenToSession } from '@/utils/index';
import { getToken } from '@/utils/auth';
import qs from 'qs';

// 获取当前登录后重定向地址
export const getRedirectUrl = (): string => {
  const { search, pathname, origin } = window.location;
  // 重定向中不能出现的数据key
  const excludeKey = ['appId', 'token'];
  const params = search ? qs.parse(search.substr(1)) : {};
  excludeKey.forEach((key) => {
    if (params[key]) {
      delete params[key];
    }
  });
  let url = `${origin + pathname}?${qs.stringify(params)}`;
  // // 目前不支持参数
  url = `${origin + pathname}`;
  return url;
};

export const getDefaultHeaderConfig = () => {
  setAppUserTokenToSession();
  const { VITE_APP_ID } = getAppEnvConfig();
  return {
    'X-appId': VITE_APP_ID,
    'x-user-token': getToken(),
    redirectUrl: getRedirectUrl()
  };
};
